const dbQuery = require('../model/db');
const {
	sendError,
	sendSuccess
} = require('../utils/sendFunction');

const getCounts = async function (req, res, next) {
	let sqlStr = 'select count(*) from user'
	let result = await dbQuery(sqlStr);
	let sqlStr1 = 'select count(*) from music';
	let result1 = await dbQuery(sqlStr1);
	let sqlStr2 = 'select count(*) from singer';
	let result2 = await dbQuery(sqlStr2);
	let sqlStr3 = 'select count(*) from gedan';
	let result3 = await dbQuery(sqlStr3);
	let sqlStr4 = 'select count(*) from mv';
	let result4 = await dbQuery(sqlStr4);
	let sqlStr5 = 'select count(*) from user where user_sex="男"';
	let result5 = await dbQuery(sqlStr5);
	let sqlStr6 = 'select count(*) from user where user_sex="女"';
	let result6 = await dbQuery(sqlStr6);
	let sqlStr7 = 'select count(*) from singer where singer_sex="男"';
	let result7 = await dbQuery(sqlStr7);
	let sqlStr8 = 'select count(*) from singer where singer_sex="女"';
	let result8 = await dbQuery(sqlStr8);
	let sqlStr9 = 'select * from singertype';
	let result9 = await dbQuery(sqlStr9);
	// 整理数据
	if (result9.length >= 0) {
		var arr = [];
		for (var i = 0; i < result9.length; i++) {
			let results = await dbQuery('select count(*) from singer where singertype_id=?', result9[i]["singertypeid"]);
			arr.push({
				name: result9[i]["singertype_name"],
				count: results[0]["count(*)"],
			})
		}
	} else {
		var arr = [];
	}
	let sqlStr10 = 'select * from musictype';
	let result10 = await dbQuery(sqlStr10);
	// 整理数据
	if (result10.length >= 0) {
		var arr1 = [];
		for (var i = 0; i < result10.length; i++) {
			let results = await dbQuery('select count(*) from music where musictype_id=?', result10[i]["musictypeid"]);
			arr1.push({
				name: result10[i]["musictype_name"],
				count: results[0]["count(*)"],
			})
		}
	} else {
		var arr1 = [];
	}
	if (result.length == 1 && result1.length == 1 && result7.length == 1 && result8.length == 1 && result2.length == 1 && result3.length == 1 && result4.length == 1 && result5.length == 1 && result6.length == 1) {
		sendSuccess(res, '获取数据成功!', {
			data: {
				userCount: result[0]["count(*)"],
				musicCount: result1[0]["count(*)"],
				singerCount: result2[0]["count(*)"],
				gedanCount: result3[0]["count(*)"],
				mvCount: result4[0]["count(*)"],
				nanUserCount: result5[0]["count(*)"],
				nvUserCount: result6[0]["count(*)"],
				nanSingerCount: result7[0]["count(*)"],
				nvSingerCount: result8[0]["count(*)"],
				musicTypeArr: arr,
				singerTypeArr: arr1
			}
		});
	} else {
		sendError(res, 500, '获取数据失败，请稍后再试!');
	}
}

module.exports = {
	getCounts
}